<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title am-cf fl">我的插件 </div>
                    <div class="fr">
                        <a href="javascript:void(0);" class="hema-login am-btn am-btn-secondary am-radius am-btn-sm">
                            <i class="am-icon-user"></i> 用户信息
                        </a>
                    </div>
                </div>
                <div class="widget-body am-fr">
                    <div class="am-scrollable-horizontal am-u-sm-12">
                        <table width="100%" class="am-table am-table-hover tpl-table-black">
                            <thead>
                            <tr>
                                <th>图标</th>
                                <th>名称</th>
                                <th>标识</th>
                                <th>介绍</th>
                                <th>作者</th>
                                <th>版本</th>
								<th>状态</th>
								<th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {if sizeof($list)}
                                {foreach $list as $item}
                                <tr>
                                    <td class="am-text-middle">
                                        <a href="https://www.hemaphp.com/{:isset($item['addon'])?'index/addon/detail?id=' . $item['addon']['addon_id']:''}" target="_blank">
                                        <img src="{$item['addon']['logo']['url']|default='/assets/img/no_pic.jpg'}" width="75" height="50" />
                                        </a>
                                    </td>
                                    <td class="am-text-middle">{$item['title']}</td>
                                    <td class="am-text-middle">{$item['name']}</td>
                                    <td class="am-text-middle">
                                        <p class="item-title">{$item['description']}</p>
                                    </td>
                                    <td class="am-text-middle">{$item['author']}</td>
                                    <td class="am-text-middle">V{$item['version']}</td>
									<td class="am-text-middle">
									        {if isset($item['status'])}
                                            <span class="item-status am-badge x-cur-p {$item['status'] ? ' am-badge-success' : ' am-badge-warning'}"
                                                 data-status="{$item['status']}"
                                                 data-name="{$item['name']}">
                                            {$item['status'] == 1 ? '启用' : '禁用'}
                                            </span>
                                            {/if}
                                    </td>
                                    <td class="am-text-middle">
                                        <div class="tpl-table-black-operation">
                                            {if isset($item['config']) AND $item['config'] == 1}
                                            <a class="item-config tpl-table-black-operation-default"
                                                href="javascript:void(0);"
                                                data-name="{$item['name']}"
                                                data-title="{$item['title']}">
												<i class="am-icon-pencil"></i> 配置
                                            </a>
                                            {/if}
                                            <a class="item-uninstall tpl-table-black-operation-del"
                                                href="javascript:void(0);"
                                                data-name="{$item['name']}">
        										<i class="am-icon-trash"></i> 卸载
                                            </a>
                                            {if isset($item['addon']) AND $item['addon']['version'][0]['version'] != $item['version']}
                                            <a class="item-upgrade tpl-table-black-operation-primary"
                                                href="javascript:void(0);"
                                                data-name="{$item['name']}">
        										<i class="am-icon-cloud-upload"></i> 升级
                                            </a>
                                            {/if}
                                        </div>
                                    </td>
                                </tr>
                                {/foreach}
                            {else /}
                                <tr>
                                    <td colspan="8" class="am-text-center">暂无记录</td>
                                </tr>
                            {/if}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="addon/tpl/config" /}
{include file="addon/tpl/login" /}
{include file="addon/tpl/user" /}
<script>
    $(function () {
        var user = <?= json_encode($addonuser)?>;
        //插件配置
        $('.item-config').on('click', function () {
            var data = $(this).data();
            $.get("{:url('addon/config')}?name=" + data.name, function (result) {
				if(result.code == 0){
				    $.show_error(result.msg);
				    return false;
				}
                $.showAction({
                    title: '插件配置 《' + data.title + '》', 
                    area: '750px', 
                    content: template('tpl-config', {config:result.data}), 
                    uCheck: true, 
                    success: function ($content) {
                        $('#my-form').formPost({
                            url: "{:url('addon/config')}?name=" + data.name
                        });
                    }, 
                    btn2: function ($content) {
                        return true;
                    }
                });
            });
        });
        
        // 安装操作
        $('.item-install').click(function () {
            var data = $(this).data();
            layer.confirm('您确定要安装该插件？', {
                    title: '友情提示',
                    icon: 0,
                    btn:['确定','取消'],
                    skin: 'layui-layer-hema'
                }, 
                function () {
					$.post("{:url('addon/install')}", data, function (result) {
						if(result.code == 1){
						    $.show_success(result.msg, result.url);
						}
						if(result.code == 0){
						   $.show_error(result.msg);
						}
						if(result.code == -1){
						   userLogin();
						}
						if(result.code == -10){
						    layer.open({
                                type: 2,
                                title: '立即支付',
                                shadeClose: true,
                                shade: false,
                                maxmin: true, //开启最大化最小化按钮
                                area: ['450px', '620px'],
                                content: "{:url('addon/pay')}?addon_id=" + result.data.addon_id
                            });
						}
					});
					layer.close();
                }
            );
            
        });
        
        //升级操作 
        $('.item-upgrade').click(function () {
            var data = $(this).data();
            layer.confirm('您确定要进行升级操作', {
                    title: '友情提示',
                    icon: 0,
                    btn:['确定','取消'],
                    skin: 'layui-layer-hema'
                }, 
                function () {
					$.post("{:url('addon/upgrade')}", data, function (result) {
						result.code === 1 ? $.show_success(result.msg, result.url) : $.show_error(result.msg);
					});
					layer.close();
                }
            );
            
        });
        
        // 卸载操作
        $('.item-uninstall').click(function () {
            var data = $(this).data();
            layer.confirm('您确定要卸载该插件？卸载后将清楚与该插件相关的所有数据！', {
                    title: '友情提示',
                    icon: 0,
                    btn:['确定','取消'],
                    skin: 'layui-layer-hema'
                }, 
                function () {
					$.post("{:url('addon/uninstall')}", data, function (result) {
						result.code === 1 ? $.show_success(result.msg, result.url) : $.show_error(result.msg);
					});
					layer.close();
                }
            );
            
        });
        
        // 状态操作
        $('.item-status').click(function () {
            var data = $(this).data();
            var msg = '禁用';
            if(parseInt(data.status) === 0){
                msg = '启用';
            }
            layer.confirm('您确定要' + msg + '该插件？', {
                    title: '友情提示',
                    icon: 0,
                    btn:['确定','取消'],
                    skin: 'layui-layer-hema'
                }, 
                function () {
					$.post("{:url('addon/status')}", data, function (result) {
						result.code === 1 ? $.show_success(result.msg, result.url) : $.show_error(result.msg);
					});
					layer.close();
                }
            );
        });
        // 检测是否登录
        $('.hema-login').click(function () {
            $.post("{:url('addon/checkLogin')}", {}, function (result) {
				if(result.code === 1){
				    userDetail();
				}else{
				    userLogin();
				}
            });
        });
        function userDetail(){
            $.showAction({
                title: '会员信息', 
                area: '460px', 
                content: template('tpl-user', {user: user}), 
                btn: ['关闭', '退出'],
                uCheck: true, 
                success: function ($content) {
                    $('#my-form').formPost({
                        url: "{:url('addon/logout')}"
                    });
                }, 
                btn2: function ($content) {
                    return true;
                }
            });
        }
        function userLogin(){
            $.showAction({
                title: '用户登录', 
                area: '460px', 
                content: template('tpl-login', {}), 
                btn: ['关闭', '登录'],
                uCheck: true, 
                success: function ($content) {
                    $('#my-form').formPost({
                        url: "{:url('addon/login')}"
                    });
                }, 
                btn2: function ($content) {
                    return true;
                }
            });
        }
    });
</script>

